package com.platform.system.service.impl;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.platform.common.constant.Constants;
import com.platform.common.constant.HttpStatus;
import com.platform.common.core.domain.AjaxResult;
import com.platform.common.core.page.TableDataInfo;
import com.platform.common.enums.ResultEnum;
import com.platform.common.exception.BusinessException;
import com.platform.common.utils.bean.BeanUtils;
import com.platform.common.utils.uuid.SnowflakeIdWorker;
import com.platform.system.domain.dto.device.ModelAppInfoDto;
import com.platform.system.domain.entity.device.ModelAppInfo;
import com.platform.system.domain.vo.device.ModelAppInfoVo;
import com.platform.system.enums.ModelTypeEnum;
import com.platform.system.logic.AiLogic;
import com.platform.system.mapper.device.ModelAppInfoMapper;
import com.platform.system.model.BaseModelHandle;
import com.platform.system.service.ISysModelAppInfoService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author Huangkai
 */
@Slf4j
@Service
public class SysModelAppInfoServiceImpl extends ServiceImpl<ModelAppInfoMapper, ModelAppInfo> implements ISysModelAppInfoService {

    @Resource
    ModelAppInfoMapper modelAppInfoMapper;

    @Resource
    ISysModelAppInfoService sysModelAppInfoService;

    @Resource
    AiLogic aiLogic;

    @Override
    public TableDataInfo selectModelAppInfoList(ModelAppInfoVo modelAppInfoVo) {
        LambdaQueryWrapper<ModelAppInfo> modelAppInfoLambdaQueryWrapper = new QueryWrapper<ModelAppInfo>().lambda();
        modelAppInfoLambdaQueryWrapper.like(StringUtils.isNotEmpty(modelAppInfoVo.getName()), ModelAppInfo::getName, modelAppInfoVo.getName());
        modelAppInfoLambdaQueryWrapper.orderByDesc(ModelAppInfo::getCreateTime);
        Page<ModelAppInfo> page = new Page<>();
        page.setCurrent(modelAppInfoVo.getPageNum());
        page.setSize(modelAppInfoVo.getPageSize());
        IPage<ModelAppInfo> modelAppInfoIPage = modelAppInfoMapper.selectPage(page, modelAppInfoLambdaQueryWrapper);
        TableDataInfo rspData = new TableDataInfo();
        List<ModelAppInfo> list =  modelAppInfoIPage.getRecords();
        List<ModelAppInfoDto> modelAppInfoDtoList = new ArrayList<>();
        if(list != null || list.size() > 0){
            String array = JSONArray.toJSONString(list);
            modelAppInfoDtoList = JSONArray.parseArray(array, ModelAppInfoDto.class);
        }
        rspData.setRows(modelAppInfoDtoList);
        rspData.setTotal(modelAppInfoIPage.getTotal());
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setMsg("查询成功");
        return rspData;
    }

    @Override
    public List<ModelAppInfoDto> selectModelAppInfoListByType(ModelAppInfoVo modelAppInfoVo) {
        List<ModelAppInfoDto> modelAppInfoDtoList = new ArrayList<>();
        LambdaQueryWrapper<ModelAppInfo> modelAppInfoLambdaQueryWrapper = new QueryWrapper<ModelAppInfo>().lambda();
        modelAppInfoLambdaQueryWrapper.eq(modelAppInfoVo.getStatus() !=null, ModelAppInfo::getStatus, modelAppInfoVo.getStatus());
        modelAppInfoLambdaQueryWrapper.eq(modelAppInfoVo.getModelType() !=null, ModelAppInfo::getModelType, modelAppInfoVo.getModelType());
        List<ModelAppInfo> modelAppInfoList = modelAppInfoMapper.selectList(modelAppInfoLambdaQueryWrapper);
        if(!CollectionUtils.isEmpty(modelAppInfoList)) {
            String array = JSONArray.toJSONString(modelAppInfoList);
            modelAppInfoDtoList = JSONArray.parseArray(array, ModelAppInfoDto.class);
        }
        return modelAppInfoDtoList;
    }

    @Override
    public ModelAppInfoDto selectModelAppInfo(String uid) {
        LambdaQueryWrapper<ModelAppInfo> modelAppInfoLambdaQueryWrapper = new QueryWrapper<ModelAppInfo>().lambda();
        modelAppInfoLambdaQueryWrapper.eq(ModelAppInfo::getUid, uid);
        ModelAppInfo modelAppInfo = modelAppInfoMapper.selectOne(modelAppInfoLambdaQueryWrapper);
        if(modelAppInfo != null) {
            ModelAppInfoDto modelAppInfoDto = new ModelAppInfoDto();
            BeanUtils.copyProperties(modelAppInfo, modelAppInfoDto);
            return modelAppInfoDto;
        }
        return null;
    }

    @Override
    public List<ModelAppInfoDto> selectModelAppInfoByUidList(List<String> uidList) {
        LambdaQueryWrapper<ModelAppInfo> modelAppInfoLambdaQueryWrapper = new QueryWrapper<ModelAppInfo>().lambda();
        modelAppInfoLambdaQueryWrapper.in(!CollectionUtils.isEmpty(uidList), ModelAppInfo::getUid, uidList);
        List<ModelAppInfo> modelAppInfoList = modelAppInfoMapper.selectList(modelAppInfoLambdaQueryWrapper);
        if(modelAppInfoList != null){
            String array = JSONArray.toJSONString(modelAppInfoList);
            List<ModelAppInfoDto> modelAppInfoDtoList = JSONArray.parseArray(array, ModelAppInfoDto.class);
            return modelAppInfoDtoList;
        }
        return null;
    }

    @Override
    public AjaxResult insertModelAppInfo(ModelAppInfoVo modelAppInfoVo) {
        ModelAppInfo modelAppInfo = new ModelAppInfo();
        modelAppInfo.setUid(SnowflakeIdWorker.getSnowflakeIdWorker());
        modelAppInfo.setName(modelAppInfoVo.getName());
        modelAppInfo.setLogoUrl(modelAppInfoVo.getLogoUrl());
        modelAppInfo.setModelParam(modelAppInfoVo.getModelParam());
        modelAppInfo.setModelType(modelAppInfoVo.getModelType());
        modelAppInfo.setPromptWord(modelAppInfoVo.getPromptWord());
        modelAppInfo.setCreateTime(new Date());
        modelAppInfo.setUpdateTime(new Date());
        modelAppInfo.insert();
        return AjaxResult.success();
    }

    @Override
    public AjaxResult updateModelAppInfoStatus(ModelAppInfoVo modelAppInfoVo) {
        ModelAppInfoDto modelAppInfoDto = sysModelAppInfoService.selectModelAppInfo(modelAppInfoVo.getUid());
        if(modelAppInfoDto == null){
            throw new BusinessException(ResultEnum.DATA_NOT_EXIST);
        }
        ModelAppInfo modelAppInfo = new ModelAppInfo();
        modelAppInfo.setStatus(Constants.NUM_ZERO);
        if(modelAppInfoDto.getStatus() == Constants.NUM_ZERO){
            modelAppInfo.setStatus(Constants.NUM_ONE);
        }
        LambdaQueryWrapper<ModelAppInfo> statusUpdateWrapper = new QueryWrapper<ModelAppInfo>().lambda();
        statusUpdateWrapper.eq(ModelAppInfo::getUid, modelAppInfoVo.getUid());
        modelAppInfo.setUpdateTime(new Date());
        modelAppInfo.update(statusUpdateWrapper);
        return AjaxResult.success();
    }

    @Override
    public AjaxResult deleteModelAppInfo(ModelAppInfoVo modelAppInfoVo) {
        LambdaQueryWrapper<ModelAppInfo> modelAppInfoLambdaQueryWrapper = new QueryWrapper<ModelAppInfo>().lambda();
        modelAppInfoLambdaQueryWrapper.eq(ModelAppInfo::getUid, modelAppInfoVo.getUid());
        modelAppInfoMapper.delete(modelAppInfoLambdaQueryWrapper);
        return AjaxResult.success();
    }

    @Override
    public AjaxResult updateModelAppInfo(ModelAppInfoVo modelAppInfoVo) {
        log.info("修改的信息{}", modelAppInfoVo);
        ModelAppInfoDto modelAppInfoDto = sysModelAppInfoService.selectModelAppInfo(modelAppInfoVo.getUid());
        if(modelAppInfoDto == null){
            throw new BusinessException(ResultEnum.DATA_NOT_EXIST);
        }
        LambdaQueryWrapper<ModelAppInfo> modelAppInfoLambdaQueryWrapper = new QueryWrapper<ModelAppInfo>().lambda();
        modelAppInfoLambdaQueryWrapper.eq(ModelAppInfo::getUid, modelAppInfoVo.getUid());
        ModelAppInfo modelAppInfo = new ModelAppInfo();
        BeanUtils.copyProperties(modelAppInfoVo, modelAppInfo);
        modelAppInfo.setUpdateTime(new Date());
        modelAppInfo.update(modelAppInfoLambdaQueryWrapper);
        return AjaxResult.success();
    }

    @Override
    public AjaxResult isModelCollection(ModelAppInfoVo modelAppInfoVo) {
        log.info("测试连通性{}", modelAppInfoVo);
        ModelAppInfoDto modelAppInfoDto = sysModelAppInfoService.selectModelAppInfo(modelAppInfoVo.getUid());
        if(modelAppInfoDto == null){
            throw new BusinessException(ResultEnum.DATA_NOT_EXIST);
        }
        ModelTypeEnum modelTypeEnum = ModelTypeEnum.findEnumByCode(modelAppInfoDto.getModelType());
        BaseModelHandle baseModelHandle = aiLogic.getMap().get(modelTypeEnum.getCode());
        JSONObject jsonObject = JSONObject.from(modelAppInfoDto);
        boolean flag = baseModelHandle.isCollection(jsonObject);
        if(flag){
            return AjaxResult.success(ResultEnum.COLLECTION_SUCCESS.message);
        }
        return AjaxResult.error(ResultEnum.MODEL_COLLECTION_FAIL.message);
    }
}
